<template>

    <!-- 任务驳回弹窗 -->
    <el-dialog
            v-if="taskId"
            title="任务驳回"
            :visible.sync="taskRejectDialog.visible"
    >
        <Form
                ref="rejectForm"
                :form-items="formItems"
                label-width=""
        ></Form>
        <span slot="footer" class="dialog-footer">
                <el-button @click="taskRejectDialog.visible = false">取 消</el-button>
                <el-button type="primary" @click="doTaskReject" :loading="taskRejectDialog.submitLoading">确 定</el-button>

            </span>
    </el-dialog>
</template>

<script>
    import Form from '../../../../common-util/src/components/page/Form.vue'
    export default {
        name: "TaskRejectDialog",
        components:{
            Form
        },
        props:{
            taskId:{
                type: String,
            },
            onSuccess:{
                type: Function
            }
        },
        computed:{
            formItems(){
                return [
                    {
                        field: {
                            name: 'toDoTaskId',
                            value: this.taskId
                        }
                    },
                    {
                        field: {
                            name: 'historyTaskId'
                        },
                        element:{
                            type: 'select',
                            select:{
                                url: this.$baseURL.workflow + '/workflow/task/tasks/' + this.taskId + '/historyTasksThatCanReject/list',
                            },
                            label: '历史任务节点',
                            required: true
                        }
                    },
                ]
            }
        },
        data(){
            return {
                taskRejectDialog:{
                    visible: false,
                    submitLoading: false,

                }
            }
        },
        methods:{
            show(){
                this.taskRejectDialog.visible = true
            },
            doTaskReject(){
                this.$refs.rejectForm.getFormRef().validate((valid) => {
                    if (valid) {
                        let form = this.$refs.rejectForm.getFormData()
                        form.toDoTaskId = this.taskId
                        this.taskRejectDialog.submitLoading = true
                        this.axios.post(this.$baseURL.workflow + '/workflow/task/tasks/reject',
                            form).then(res =>{
                            this.taskRejectDialog.visible = false
                            this.$message.success('任务驳回成功')
                            // 重新加载数据
                            if(this.onSuccess){
                                this.onSuccess()
                            }
                        }).catch(error => {
                            if(error.response){
                                this.$message.error(error.response.data.msg)
                            }else {
                                this.$message.error('网络错误或服务不可用')
                            }
                        }).finally(()=>{
                            this.taskRejectDialog.submitLoading = false
                        })
                    } else {
                        return false;
                    }
                });
            }
        }

    }
</script>

<style scoped>

</style>